macos: fix the position of curosr of input method for macos.
authorZhi <yfwz100@yeah.net>
Mon, 21 Jun 2021 23:20:53 +0000 (07:20 +0800)
committerZhi <yfwz100@yeah.net>
Tue, 22 Jun 2021 14:57:34 +0000 (22:57 +0800)
In the refactoring from GdkWindow to GdkSurface, GtkWidget no longer
corresponds to a GdkSurface. We have to calculate the relative position
from GtkWidget to the GdkSurface.

Closes #4063.

gtk/gtkimcontextquartz.c

index 62ca2c4e5679874d3c88cd5e19ffd919143e264f..fc1e1ff3a8126fe17bbb529d664c57ac12675a7e 100644 (file)
@@ -41,6 +41,7 @@ typedef struct _GtkIMContextQuartz
   GtkIMContext parent;
   GtkIMContext *helper;
   GdkSurface *client_surface;
+  GtkWidget *client_widget;
   char *preedit_str;
   unsigned int cursor_index;
   unsigned int selected_len;
@@ -266,6 +267,7 @@ quartz_set_client_surface (GtkIMContext *context,
 
   GTK_NOTE (MODULES, g_print ("quartz_set_client_surface: %p\n", widget));
 
+  qc->client_widget = widget;
   qc->client_surface = NULL;
 
   if (widget != NULL)
@@ -302,26 +304,25 @@ static void
 quartz_set_cursor_location (GtkIMContext *context, GdkRectangle *area)
 {
   GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context);
-  int x, y;
+  int sx, sy;
+  double wx, wy;
 
   GTK_NOTE (MODULES, g_print ("quartz_set_cursor_location\n"));
 
-  if (!qc->client_surface)
+  if (!qc->client_surface || !qc->client_widget)
     return;
 
   if (!qc->focused)
     return;
 
-  qc->cursor_rect->x = area->x;
-  qc->cursor_rect->y = area->y;
+  gdk_surface_get_origin (qc->client_surface, &sx, &sy);
+  gtk_widget_translate_coordinates(qc->client_widget, qc->client_surface->widget, area->x, area->y, &wx, &wy);
+  
+  qc->cursor_rect->x = sx + (int) wx;
+  qc->cursor_rect->y = sy + (int) wy;
   qc->cursor_rect->width = area->width;
   qc->cursor_rect->height = area->height;
 
-  gdk_surface_get_origin (qc->client_surface, &x, &y);
-
-  qc->cursor_rect->x = area->x + x;
-  qc->cursor_rect->y = area->y + y;
-
   if (!GDK_IS_MACOS_SURFACE (qc->client_surface))
     return;